﻿<MudDataGrid @ref="_dataGrid" Items="@_items" Sortable="true">
    <Columns>
        <PropertyColumn Property="x => x.Name" Comparer="@(_naturalSortingEnabled ? new MudBlazor.Utilities.NaturalComparer() : null)" />
        <PropertyColumn Property="x => x.Value" />
        <PropertyColumn Property="x => x.Misc" />
    </Columns>
</MudDataGrid>
<MudButton OnClick="CallSort" Variant="Variant.Outlined">Set Sort on Name Column</MudButton>
<MudSwitch @bind-Value="@_naturalSortingEnabled">Enable Natural Sorting</MudSwitch>

@code {
    public static string __description__ = @"Test in browser custom sorting of DataGrid. Note that the 'Name' column is being sorted using a natural sorting algorithm.";
    private MudDataGrid<Item> _dataGrid = null!;

    private bool _naturalSortingEnabled = true;

    private readonly IEnumerable<Item> _items = new List<Item>
    {
        new("1", 42, "555"), 
        new("10", 73, "7"), 
        new("2", 11, "4444"), 
        new("1_10", 33, "33333"),
        new("1_2", 99, "66"), 
        new("1_11", 44, "1111111"),
        new("0", 55, "222222")
    };

    private async Task CallSort()
    {
        await _dataGrid.SetSortAsync(nameof(Item.Name), SortDirection.Ascending, x => x.Name, new MudBlazor.Utilities.NaturalComparer());
    }

    public record Item(string Name, int Value, string Misc);

}
